﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.SweptVolume.CApSVHoldingObjectValidator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.Collections.ObjectModel;
using System.Resources;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.SweptVolume;

internal class CApSVHoldingObjectValidator : ITxValidator
{
  private ResourceManager m_resourceManager;
  private bool m_allowRoboticOperations;
  private bool m_allowRoboticPrograms;
  private bool m_allowObjectFlowOperations;
  private bool m_validateEmptyName;
  private bool m_checkCanBeModified;

  public CApSVHoldingObjectValidator(bool checkCanBeModified = false)
  {
    this.m_resourceManager = new ResourceManager("DnProcessSimulateCommands.SweptVolume.StringTable", this.GetType().Assembly);
    this.m_allowRoboticOperations = true;
    this.m_allowRoboticPrograms = true;
    this.m_allowObjectFlowOperations = true;
    this.m_checkCanBeModified = checkCanBeModified;
  }

  public bool AllowObjectFlowOperations
  {
    set => this.m_allowObjectFlowOperations = value;
    get => this.m_allowObjectFlowOperations;
  }

  public bool AllowRoboticOperations
  {
    set => this.m_allowRoboticOperations = value;
    get => this.m_allowRoboticOperations;
  }

  public bool AllowRoboticPrograms
  {
    set => this.m_allowRoboticPrograms = value;
    get => this.m_allowRoboticPrograms;
  }

  public bool ValidateEmptyName
  {
    get => this.m_validateEmptyName;
    set => this.m_validateEmptyName = value;
  }

  public ITxObject GetObject(string text)
  {
    ITxObject itxObject1 = (ITxObject) null;
    TxObjectList txObjectList = (TxObjectList) null;
    if (text != null)
      txObjectList = TxApplication.ActiveDocument.GetObjectsByName(text);
    if (txObjectList != null && ((Collection<ITxObject>) txObjectList).Count > 0)
    {
      foreach (ITxObject itxObject2 in (Collection<ITxObject>) txObjectList)
      {
        if (this.IsValidObject(itxObject2, out string _))
        {
          itxObject1 = itxObject2;
          break;
        }
      }
    }
    return itxObject1;
  }

  public string GetText(ITxObject obj)
  {
    string text = (string) null;
    if (obj != null)
      text = obj.Name;
    return text;
  }

  public bool IsValidObject(ITxObject obj, out string errorMessage)
  {
    using (new TxCallMessages.Collector((object) "CApSVHoldingObjectValidator.IsValidObject"))
    {
      errorMessage = (string) null;
      bool flag1;
      if (obj != null)
      {
        if (CApSVHoldingObjectValidator.isObjectFlowOperation(obj) && !this.m_allowObjectFlowOperations || CApSVHoldingObjectValidator.isRoboticOperation(obj) && !this.m_allowRoboticOperations || CApSVHoldingObjectValidator.isRoboticProgram(obj) && !this.m_allowRoboticPrograms)
        {
          flag1 = false;
          errorMessage = this.m_resourceManager.GetString("SV_INVALID_OBJECT");
        }
        else
        {
          bool flag2 = CApSVHoldingObjectValidator.isObjectValidForHoldingSweptVolume(obj);
          flag1 = this.m_checkCanBeModified ? flag2 && obj.CanBeModified : flag2;
          if (!flag1)
            errorMessage = !TxCallMessages.IsEmpty() ? string.Join("\n", TxCallMessages.GetMessages()) : this.m_resourceManager.GetString("SV_INVALID_OBJECT");
        }
      }
      else
      {
        flag1 = false;
        errorMessage = this.m_resourceManager.GetString("SV_NO_SUCH_OBJECT");
      }
      return flag1;
    }
  }

  public bool IsValidText(string text, out string errorMessage)
  {
    bool flag = false;
    errorMessage = (string) null;
    if (string.IsNullOrEmpty(text))
    {
      if (!this.ValidateEmptyName)
        errorMessage = this.m_resourceManager.GetString("SV_MISSING_NAME");
      else
        flag = true;
    }
    else if (this.GetObject(text) != null)
      flag = true;
    else
      errorMessage = !this.DoesExistObjectByName(text) ? this.m_resourceManager.GetString("SV_INVALID_NAME") : this.m_resourceManager.GetString("SV_INVALID_OBJECT");
    return flag;
  }

  internal static bool isObjectValidForHoldingSweptVolume(ITxObject obj)
  {
    bool flag = false;
    if (obj is ITxSweptVolumeHoldingObject)
    {
      if (CApSVHoldingObjectValidator.isRoboticOperation(obj))
      {
        if (obj is ITxRoboticOrderedCompoundOperation compoundOperation)
          flag = CApSVHoldingObjectValidator.doesRoboticOperationHaveRobot(compoundOperation) && CApSVHoldingObjectValidator.hasLocations(compoundOperation as ITxOrderedCompoundOperation);
      }
      else if (CApSVHoldingObjectValidator.isObjectFlowOperation(obj))
      {
        if (!CApSVHoldingObjectValidator.hasObjectFlowOperationSimulatedObject(obj as TxObjectFlowOperation))
          return false;
        flag = CApSVHoldingObjectValidator.hasLocations(obj as ITxOrderedCompoundOperation);
      }
      else if (CApSVHoldingObjectValidator.isRoboticProgram(obj))
        flag = !CApSVHoldingObjectValidator.isRoboticProgramEmpty(obj as ITxRoboticProgram) && CApSVHoldingObjectValidator.doesRoboticProgramHaveLocations(obj as ITxRoboticProgram);
    }
    return flag;
  }

  private static bool isRoboticOperation(ITxObject op)
  {
    bool flag = false;
    if (op != null && op is ITxRoboticOrderedCompoundOperation)
      flag = true;
    return flag;
  }

  private static bool doesRoboticOperationHaveRobot(ITxRoboticOrderedCompoundOperation robOp)
  {
    return robOp.Robot != null;
  }

  private static bool hasLocations(ITxOrderedCompoundOperation op)
  {
    bool flag = false;
    if (op != null && ((Collection<ITxObject>) ((ITxObjectCollection) op).GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITxLocationOperation)))).Count >= 2)
      flag = true;
    return flag;
  }

  private static bool hasObjectFlowOperationSimulatedObject(TxObjectFlowOperation flowOp)
  {
    bool flag = false;
    if (TxObjectFlowOperation.op_Inequality(flowOp, (ITxObject) null))
    {
      try
      {
        if (flowOp.SimulatedObject != null)
          flag = true;
      }
      catch (TxPartHasNoAppearanceException ex)
      {
        flag = false;
      }
    }
    return flag;
  }

  private static bool isObjectFlowOperation(ITxObject op)
  {
    bool flag = false;
    if (op != null && op is TxObjectFlowOperation)
      flag = true;
    return flag;
  }

  private static bool isRoboticProgram(ITxObject obj)
  {
    bool flag = false;
    if (obj != null && obj is ITxRoboticProgram)
      flag = true;
    return flag;
  }

  private static bool isRoboticProgramEmpty(ITxRoboticProgram prog)
  {
    bool flag = false;
    if (prog != null && ((Collection<ITxObject>) prog.GetDirectChildElements((ITxTypeFilter) new TxNoTypeFilter())).Count == 0)
      flag = true;
    return flag;
  }

  private static bool doesRoboticProgramHaveLocations(ITxRoboticProgram prog)
  {
    bool flag = false;
    if (prog != null)
    {
      TxTypeFilter txTypeFilter1 = new TxTypeFilter(typeof (ITxOperation));
      TxTypeFilter txTypeFilter2 = new TxTypeFilter(typeof (ITxLocationOperation));
      TxObjectList directChildElements = prog.GetDirectChildElements((ITxTypeFilter) txTypeFilter1);
      int num = 0;
      foreach (ITxOperation itxOperation in (Collection<ITxObject>) directChildElements)
      {
        if (itxOperation is ITxObjectCollection)
        {
          TxObjectList allDescendants = (itxOperation as ITxObjectCollection).GetAllDescendants((ITxTypeFilter) txTypeFilter2);
          if (((Collection<ITxObject>) allDescendants).Count > 0)
          {
            num += ((Collection<ITxObject>) allDescendants).Count;
            if (num >= 2)
            {
              flag = true;
              break;
            }
          }
        }
      }
    }
    return flag;
  }

  private bool DoesExistObjectByName(string text)
  {
    bool flag = false;
    if (text != null && text.Length > 0)
    {
      TxObjectList objectsByName = TxApplication.ActiveDocument.GetObjectsByName(text);
      flag = objectsByName != null && ((Collection<ITxObject>) objectsByName).Count > 0;
    }
    return flag;
  }
}
